<?php

/**
 * ErrorController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action {
	
	protected $_error = null;
	
	public function init() {
		$this->_error = $this->getRequest ()->getParam ( 'error_handler' );
	}
	
	public function errorAction() {
		switch ( $this->_error->type) {
			case 'EXCEPTION_NO_CONTROLLER' :
			case 'EXCEPTION_NO_ACTION' :
				$this->_forward ( 'not-found' );
				return;
			case 'EXCEPTION_OTHER' :
				$this->_forward ( 'server-error' );
				return;
		}
	}
	
	public function UserInputAction() {
		if (defined ( 'APPLICATION_STATE' ) && APPLICATION_STATE == 'development') {
			Zend_Debug::dump ( $this->_error->exception );
			die ( 'DEBUG: died in ' . __FUNCTION__ );
		} else {
			$this->render ();
		}
	}
	
	public function NoAuthAction() {
		if (defined ( 'APPLICATION_STATE' ) && APPLICATION_STATE == 'development') {
			Zend_Debug::dump ( $this->_error->type );
			die ( 'DEBUG: died in ' . __FUNCTION__ );
		} else {
			$this->render ();
		}
	}
	
	public function notFoundAction() {
		if (defined ( 'APPLICATION_STATE' ) && APPLICATION_STATE == 'development') {
			echo "<pre>Error Request:\n\n</pre>";
			Zend_Debug::dump ( $this->_error->request );
			echo "<pre>Exception:\n\n</pre>";
			Zend_Debug::dump ( $this->_error->exception );
			die ( 'DEBUG: died in ' . __FUNCTION__ );
		} else {
			$this->_response->setHttpResponseCode ( 404 );
			$this->render ();
		}
	}
	
	public function serverErrorAction() {
		$exception = new Exception();
		if (defined ( 'APPLICATION_STATE' ) && APPLICATION_STATE == 'development') {
			echo "<pre>Error Request:\n\n</pre>";
			Zend_Debug::dump ( $this->_error->request );
			echo "<pre>Exception:\n\n</pre>";
			Zend_Debug::dump ( $this->_error->exception );
			die ( 'DEBUG: died in ' . __FUNCTION__ );
		} else {
			$writer = new Zend_Log_Writer_Stream ( 'application/logs/application_exception.log' );
			$logger = new Zend_Log ( $writer );
			$logger->log ( get_class ( $exception ) . ' -> ' . $exception->getMessage (), Zend_Log::CRIT );
			$this->_response->setHttpResponseCode ( 500 );
			$this->_response->setBody ( $this->render () );
		}
		$this->render();
	}

}
